const URL = 'https://douban.uieee.com/v2/movie'

// 根据不同条目获取电影列表
function findList(type, page = 1, count = 20, cb, fail_cb) {
  wx.showNavigationBarLoading()
  wx.request({
    url: `${URL}/${type}`,
    method: 'GET',
    dataType: 'json',
    data: {
      // city: getApp().data.currentCity,
      start: (page - 1) * count,
      count: count
    },
    header: {
      'content-type': 'json'
    },
    success: function (res) {
      console.log('findList返回数据：')
      console.log(res)
      if (res.data.code === 112) {
        wx.showLoading({
          title: '请求接口出错',
          mask: true
        })
      } else {
        typeof cb === 'function' && cb(res)
        wx.hideNavigationBarLoading()
      }
    },
    fail: function (err) {
      console.log('findList返回错误：')
      console.log(err)
      wx.showLoading({
        title: '网络出错啦',
        mask: true
      })
      typeof fail_cb === 'function' && fail_cb(err)
    }
  })
}
// 根据id获取电影详情
function findOne(id, cb, fail_cb) {
  wx.showNavigationBarLoading()
  wx.request({
    url: `${URL}/subject/${id}`,
    method: 'GET',
    dataType: 'json',
    header: {
      'content-type': 'json'
    },
    success: function (res) {
      console.log('findOne返回数据：')
      console.log(res)
      if (res.data.code === 112) {
        wx.showLoading({
          title: '请求接口出错',
          mask: true
        })
      } else {
        typeof cb === 'function' && cb(res)
        wx.hideNavigationBarLoading()
      }
    },
    fail: function (err) {
      console.log('findOne返回错误：')
      console.log(err)
      wx.showLoading({
        title: '网络出错啦',
        mask: true
      })
      typeof fail_cb === 'function' && fail_cb(err)
    }
  })
}
// 根据id获取电影短评comments或影评reviews
function findComments(type, id, page = 1, count = 20, cb, fail_cb) {
  wx.showNavigationBarLoading()
  wx.request({
    url: `${URL}/subject/${id}/${type}`,
    method: 'GET',
    dataType: 'json',
    data: {
      start: (page - 1) * count,
      count: count
    },
    header: {
      'content-type': 'json'
    },
    success: function (res) {
      console.log('findComments返回数据：')
      console.log(res)
      if (res.data.code === 112) {
        wx.showLoading({
          title: '请求接口出错',
          mask: true
        })
      } else {
        typeof cb === 'function' && cb(res)
        wx.hideNavigationBarLoading()
      }
    },
    fail: function (err) {
      console.log('findComments返回错误：')
      console.log(err)
      wx.showLoading({
        title: '网络出错啦',
        mask: true
      })
      typeof fail_cb === 'function' && fail_cb(err)
    }
  })
}
// 条目搜索
function search(name, page = 1, count = 20, cb, fail_cb) {
  wx.showNavigationBarLoading()
  wx.request({
    url: `${URL}/search?q=${name}`,
    method: 'GET',
    dataType: 'json',
    data: {
      start: (page - 1) * count,
      count: count
    },
    header: {
      'content-type': 'json'
    },
    success: function (res) {
      console.log('search返回数据：')
      console.log(res)
      if (res.data.code === 112) {
        wx.showLoading({
          title: '请求接口出错',
          mask: true
        })
      } else {
        typeof cb === 'function' && cb(res)
        wx.hideNavigationBarLoading()
      }
    },
    fail: function (err) {
      console.log('search返回错误：')
      console.log(err)
      wx.showLoading({
        title: '网络出错啦',
        mask: true
      })
      typeof fail_cb === 'function' && fail_cb(err)
    }
  })
}
module.exports = {
  findList,
  findOne,
  findComments,
  search
}